package com.opt.sample;

import java.awt.BorderLayout;
import java.awt.Container;
import java.lang.reflect.InvocationTargetException;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

public class ProgressBarFrame extends JFrame {

	public ProgressBarFrame(JProgressBar jProgressBar) {
		super("Pasek progresu");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(300, 100);
		setLocation(50, 50);
		setLayout(new BorderLayout());

		Container contentPane = this.getContentPane();
		contentPane.add(jProgressBar, BorderLayout.CENTER);

		Border border = BorderFactory.createTitledBorder("Optimalization...");
		jProgressBar.setBorder(border);

		this.setVisible(true);

	}

	public static class BarThread extends Thread {
		private static int DELAY = 500;

		JProgressBar progressBar;

		public BarThread(JProgressBar bar) {
			progressBar = bar;
		}

		public void run() {
			int minimum = progressBar.getMinimum();
			int maximum = progressBar.getMaximum();
			Runnable runner = new Runnable() {
				public void run() {
					int value = progressBar.getValue();
					progressBar.setValue(value + 1);
				}
			};
			for (int i = minimum; i < maximum; i++) {
				try {
					SwingUtilities.invokeAndWait(runner);
					Thread.sleep(DELAY);
				} catch (InterruptedException ignoredException) {
				} catch (InvocationTargetException ignoredException) {
				}
			}
		}
	}
}
